package com.epalmpay.util;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.epalmpay.bean.OilMessageBean;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.JedisShardInfo;

public class RedisClient {

	
	public static RedisClient redisClient;
	
	
  private static	final String URI="http://user:terminal_2016@121.201.20.73:16379/0";
    
    private static int DBNUMBER=0;
    
    //可用连接实例的最大数目，默认值为8；
    //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
    private static int MAX_ACTIVE = 1024;
    
    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
    private static int MAX_IDLE = 200;
    
    //等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
    private static int MAX_WAIT = 20000;
    
    private static int TIMEOUT = 20000;
    
    //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static boolean TEST_ON_BORROW = true;
    
    
    private Jedis jedis;//非切片额客户端连接
    private JedisPool jedisPool;//非切片连接池
    private ShardedJedis shardedJedis;//切片额客户端连接  
    private ShardedJedisPool shardedJedisPool;//切片连接池
    
    private  RedisClient() 
    { 
        try {
			initialPool();
			initialShardedPool(); 
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
       
        shardedJedis = shardedJedisPool.getResource(); 
        jedis = jedisPool.getResource(); 
        
        
    } 
    
    public static  RedisClient getInstance(){
    	if(redisClient==null){
    		redisClient=new RedisClient();
    	}
    	return redisClient;
    }
 
    /**
     * 初始化非切片池
     * @throws URISyntaxException 
     */
    private void initialPool() throws URISyntaxException 
    { 
        // 池基本配置 
    	JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(MAX_ACTIVE);
        config.setMaxIdle(MAX_IDLE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setTestOnBorrow(TEST_ON_BORROW);
        jedisPool = new JedisPool(config,new java.net.URI(URI));
    }
    
   
    	 
    
    /** 
     * 初始化切片池 
     * @throws URISyntaxException 
     */ 
    private void initialShardedPool() throws URISyntaxException 
    { 
        // 池基本配置 
        JedisPoolConfig config = new JedisPoolConfig(); 
        config.setMaxIdle(20); 
        config.setMaxTotal(100);
        config.setMaxWaitMillis(10000); 
        config.setTestOnBorrow(true);        
        // slave链接 
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); 
        shards.add(new JedisShardInfo(new java.net.URI(URI))); 

        // 构造池 
        shardedJedisPool = new ShardedJedisPool(config, shards); 
    } 


      public void addMessage(OilMessageBean messageBean) {
    	  String content=JSONObject.toJSONString(messageBean);
    	  System.out.println("addMessage...."+content);
    	  shardedJedis.lpush("pushList_"+DBNUMBER,content ); 

      }

    
}